package com.menggudf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.menggudf.entity.Symptom;

@Repository
public class SymptomDao{

    private static final Log log = LogFactory.getLog(SymptomDao.class);

    @PersistenceContext(unitName = "medicalUnit")
    private EntityManager entityManager;

    public void persist(Symptom transientInstance) {
        try {
            entityManager.persist(transientInstance);
        } catch (RuntimeException re) {
            log.error("persist failed", re);
            throw re;
        }
    }

    public void remove(Symptom persistentInstance) {
        try {
            entityManager.remove(persistentInstance);
        } catch (RuntimeException re) {
            log.error("remove failed", re);
            throw re;
        }
    }

    public Symptom merge(Symptom detachedInstance) {
        try {
            Symptom result = entityManager.merge(detachedInstance);
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public Symptom findById(Long id) {
        try {
            Symptom instance = entityManager.find(Symptom.class, id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }

    public List<Symptom> getByID(Integer id) {
        // TODO Auto-generated method stub
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Symptom> findBySymptom(String ids, String symptom) {
        try {
            StringBuffer sqld = new StringBuffer("from Symptom s where s.illness in ("+ids+") and s.symptom=?1 ");
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, symptom);
            return query.getResultList();
        } catch (Exception e) {
            log.error("findBySymptom fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<String> getSympByLike(String symp) {
        try {
            StringBuffer sqld = new StringBuffer("select s.symptom from Symptom s where s.symptom like:symp");
            Query query = entityManager.createQuery(sqld.toString()).setParameter("symp", "%"+symp+"%");
            return query.getResultList();
        } catch (Exception e) {
            log.error("getSympByLike fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<Integer> findHisTorySortsBySymptom(String symptom) {
        try {
            StringBuffer sqld = new StringBuffer("select distinct s.historySort from Symptom s where s.symptom=?1 ");
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, symptom);
            return query.getResultList();
        } catch (Exception e) {
            log.error("findHisTorySortsBySymptom fail",e);
            throw e;
        }
    }

    public boolean hasSymptom(String symptom) {
        try {
            StringBuffer sqld = new StringBuffer("from Symptom s where s.symptom=?1");
            Query query = entityManager.createQuery(sqld.toString()).setParameter(1, symptom);
            List<Symptom> list = query.getResultList();
            if(list!=null && list.size()>0){
                return true;
            }else{
                return false;
            }
        } catch (Exception e) {
            log.error("hasSymptom fail",e);
            throw e;
        }
    }

    @SuppressWarnings("unchecked")
    public List<String> getSymptomList(String symptom, int pageSize, int offset) {
        try {
            StringBuffer sqld = new StringBuffer("select distinct s.symptom from Symptom s");
            if(!StringUtils.isEmpty(symptom)){
                sqld.append(" where s.symptom like:name");
            }
            Query query = entityManager.createQuery(sqld.toString());
            if(!StringUtils.isEmpty(symptom)){
                query.setParameter("name", "%"+symptom+"%");
            }
            query.setFirstResult(offset).setMaxResults(pageSize);
            return query.getResultList();
        } catch (Exception e) {
            log.error("getSymptomList fail",e);
            throw e;
        }
    }

    public long getSymptomListCount(String symptom) {
        try {
            StringBuffer sqld = new StringBuffer("select distinct s.symptom from Symptom s");
            if(!StringUtils.isEmpty(symptom)){
                sqld.append("  where s.symptom like:name");
            }
            Query query = entityManager.createQuery(sqld.toString());
            if(!StringUtils.isEmpty(symptom)){
                query.setParameter("name", symptom);
            }
            return query.getResultList().size();
        } catch (Exception e) {
            log.error("getSymptomListCount fail",e);
            throw e;
        }
    }
}
